package com.qf.lja.servlet;

import com.qf.lja.dao.CartDao;
import com.qf.lja.enity.Cart;
import com.qf.lja.enity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@WebServlet(urlPatterns = "/cartShow.do")
public class CartShowServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //utf-8
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //判断是否登录
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            resp.sendRedirect("login.jsp");
            return;
        }

        Integer userId = user.getUserId();

        CartDao cartDao = new CartDao();
        List<Cart> carts = cartDao.findAllByUserId(userId);

        req.setAttribute("cartList", carts);
        req.getRequestDispatcher("/cart.jsp").forward(req, resp);
    }
}
